﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Threading.Tasks;
using WalkingTec.Mvvm.Core;
using WalkingTec.Mvvm.Mvc;
using WalkingTec.Mvvm.Core.Extensions;
using System.Linq;
using System.Collections.Generic;
using nERP.Model.Po;
using nERP.Model.Bas;
using nERP.Model.Wms;
using nERP.ViewModel.Wms.BuyInWarehouseVMs;
using nERP.Model;
using static System.Runtime.CompilerServices.RuntimeHelpers;

namespace nERP.Wms.Controllers
{
    [AuthorizeJwtWithCookie]
    [ActionDescription("_Model.BuyInWarehouse")]
    [ApiController]
    [Route("/api/Wms/BuyInWarehouse")]
    public partial class BuyInWarehouseController : BaseApiController
    {
        [ActionDescription("Sys.Get")]
        [HttpGet("{id}")]
        public IActionResult Get(string id)
        {
            var vm = Wtm.CreateVM<BuyInWarehouseVM>(id);
            return Ok(vm);
        }

        [ActionDescription("Sys.Create")]
        [HttpPost("[action]")]
        public async Task<IActionResult> Create(BuyInWarehouseVM vm)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState.GetErrorJson());
            }
            else
            {
                await vm.DoAddAsync();
                if (ModelState.IsValid)
                {
                    await vm.StartWorkflowAsync();
                }

                if (!ModelState.IsValid)
                {
                    return BadRequest(ModelState.GetErrorJson());
                }
                else
                {
                    return Ok(vm.Entity);
                }
            }

        }

        [ActionDescription("Sys.Edit")]
        [HttpPut("[action]")]
        public async Task<IActionResult> Edit(BuyInWarehouseVM vm)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState.GetErrorJson());
            }
            else
            {
                await vm.DoEditAsync(false);
                if (!ModelState.IsValid)
                {
                    return BadRequest(ModelState.GetErrorJson());
                }
                else
                {
                    return Ok(vm.Entity);
                }
            }
        }

        [ActionDescription("Sys.Approve")]
        [Public]
        [HttpPost("[action]")]
        public async Task<ActionResult> Approve(BuyInWarehouseVM vm)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState.GetErrorJson());
            }
            else
            {
                await vm.DoEditAsync(false);
                if (ModelState.IsValid)
                {
                    await vm.ContinueWorkflowAsync(vm.ActionName, vm.Remark);
                }
                if (!ModelState.IsValid)
                {
                    return BadRequest(ModelState.GetErrorJson());
                }
                else
                {
                    return Ok(vm.Entity);
                }
            }
        }


        [HttpPost("BatchEdit")]
        [ActionDescription("Sys.BatchEdit")]
        public ActionResult BatchEdit(BuyInWarehouseBatchVM vm)
        {
            if (!ModelState.IsValid || !vm.DoBatchEdit())
            {
                return BadRequest(ModelState.GetErrorJson());
            }
            else
            {
                return Ok(vm.Ids.Count());
            }
        }

        [HttpPost("BatchDelete")]
        [ActionDescription("Sys.Delete")]
        public IActionResult BatchDelete(string[] ids)
        {
            var vm = Wtm.CreateVM<BuyInWarehouseBatchVM>();
            if (ids != null && ids.Count() > 0)
            {
                vm.Ids = ids;
            }
            else
            {
                return Ok();
            }
            if (!ModelState.IsValid || !vm.DoBatchDelete())
            {
                return BadRequest(ModelState.GetErrorJson());
            }
            else
            {
                return Ok(ids.Count());
            }
        }

        [ActionDescription("Sys.DownloadTemplate")]
        [HttpGet("GetExcelTemplate")]
        public IActionResult GetExcelTemplate()
        {
            var vm = Wtm.CreateVM<BuyInWarehouseImportVM>();
            var qs = new Dictionary<string, string>();
            foreach (var item in Request.Query.Keys)
            {
                qs.Add(item, Request.Query[item]);
            }
            vm.SetParms(qs);
            var data = vm.GenerateTemplate(out string fileName);
            return File(data, "application/vnd.ms-excel", fileName);
        }

        [ActionDescription("Sys.Import")]
        [HttpPost("Import")]
        public ActionResult Import(BuyInWarehouseImportVM vm)
        {

            if (vm.ErrorListVM.EntityList.Count > 0 || !vm.BatchSaveData())
            {
                return BadRequest(vm.GetErrorJson());
            }
            else
            {
                return Ok(vm.EntityList.Count);
            }
        }




        [HttpGet("GetBuys")]
        [AllRights]
        public ActionResult GetBuys()
        {
            return Ok(DC.Set<Buy>().GetSelectListItems(Wtm, x => x.BuyCode, x => x.BuyCode.ToString()));
        }
        [HttpPost("[action]")]
        [AllRights]
        public ActionResult Select_GetBuyByBuyId(List<string> id)
        {
            var rv = DC.Set<Buy>().CheckIDs(id).GetSelectListItems(Wtm, x => x.BuyCode, x => x.BuyCode.ToString());
            return Ok(rv);
        }

        [HttpPost("[action]")]
        [AllRights]
        public ActionResult Select_GetBuyInWarehouseByBuy(List<string> id)
        {
            var rv = DC.Set<BuyInWarehouse>().CheckIDs(id, x => x.BuyId).GetSelectListItems(Wtm, x => x.BuyInWarehouseCode, x => x.BuyInWarehouseCode);
            return Ok(rv);
        }


        [HttpGet("GetSuppliers")]
        [AllRights]
        public ActionResult GetSuppliers(string BuyCode)
        {
            return Ok(DC.Set<Supplier>().Where(x => x.SupplierCode.Equals(
                DC.Set<Buy>().Where(z0 => z0.BuyCode.Equals(BuyCode)).Select(z0 => z0.SupplierId).SingleOrDefault())).GetSelectListItems(Wtm, x => x.SupplierName, x => x.SupplierCode.ToString()));
        }
        [HttpPost("[action]")]
        [AllRights]
        public ActionResult Select_GetSupplierBySupplierId(List<string> id)
        {
            var rv = DC.Set<Supplier>().CheckIDs(id).GetSelectListItems(Wtm, x => x.SupplierName, x => x.SupplierCode.ToString());
            return Ok(rv);
        }

        [HttpPost("[action]")]
        [AllRights]
        public ActionResult Select_GetBuyInWarehouseBySupplier(List<string> id)
        {
            var rv = DC.Set<BuyInWarehouse>().CheckIDs(id, x => x.SupplierId).GetSelectListItems(Wtm, x => x.BuyInWarehouseCode, x => x.BuyInWarehouseCode);
            return Ok(rv);
        }


        [HttpGet("GetFrameworkUsers")]
        [AllRights]
        public ActionResult GetFrameworkUsers()
        {
            return Ok(DC.Set<FrameworkUser>().GetSelectListItems(Wtm, x => x.Name, x => x.ITCode.ToString()));
        }
        [HttpPost("[action]")]
        [AllRights]
        public ActionResult Select_GetFrameworkUserByFrameworkUserId(List<string> id)
        {
            var rv = DC.Set<FrameworkUser>().CheckIDs(id).GetSelectListItems(Wtm, x => x.Name, x => x.ITCode.ToString());
            return Ok(rv);
        }

        [HttpPost("[action]")]
        [AllRights]
        public ActionResult Select_GetBuyInWarehouseByFrameworkUser(List<string> id)
        {
            var rv = DC.Set<BuyInWarehouse>().CheckIDs(id, x => x.MakeUserId).GetSelectListItems(Wtm, x => x.BuyInWarehouseCode, x => x.BuyInWarehouseCode);
            return Ok(rv);
        }

    }
}